package com.warm.base.design.s_composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2019/11/27 13:50
 * @Author: LChuang
 * @Description: 树
 */
public class Composite extends Component {
    private List<Component> components;

    public Composite(String name) {
        super(name);
        this.components = new ArrayList<>();
    }

    @Override
    public void add(Component component) {
        this.components.add(component);
    }

    @Override
    public void remove(Component component) {
        this.components.remove(component);
    }

    @Override
    public String display() {
        StringBuilder builder = new StringBuilder(this.name);
        for(Component c : this.components){
            builder.append("\n");
            builder.append(c.display());
        }
        System.out.println(builder.toString());
        return builder.toString();
    }
}
